import axi from 'axios'
import config from 'config'
import qs from 'qs'
import { ser } from '../utils/str'

axi.defaults.baseURL = config.baseUrl;
axi.interceptors.request.use(config => {
    if ( config.method.toUpperCase() == 'GET' )
        config.paramsSerializer = params => qs.stringify(params, { arrayFormat: 'repeat' })
});
axi.interceptors.response.use(resp => resp);

export default function (options) {
    const ret = axi.create();

    options.headers = {
        ...options['headers'],
        [config.authHttpHeader] : ser(localStorage.getItem(config.authHttpHeader))
    }

    return ret(options);
}
